package com.chat.api.listener;


import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

@WebListener
public class SessionListen implements HttpSessionListener {
    private Integer count;

    @Override
    public void sessionCreated(HttpSessionEvent event) {
//        System.out.printf("***************新建会话");
        HttpSession session = event.getSession();
        ServletContext servletContext = session.getServletContext();
        count = (Integer) servletContext.getAttribute("userCount");
        if (count == null){
            count = 0;
        }
        count++;
        servletContext.setAttribute("userCount", count);
        System.out.println(servletContext.getAttribute("userCount"));
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent event) {
//        System.out.println("session销毁1");
//        HttpSession session = event.getSession();
//        RedisTemplate redisTemplate = ContextUtil.getBean(RedisTemplate.class);
//        redisTemplate.delete(session.getId());
//        redisTemplate.delete("session"+session.getId());
//        System.out.println("session销毁完成！！！");
//        HttpSession session = event.getSession();
//        count = (Integer) session.getServletContext().getAttribute("userCount");
//        count--;
//        session.getServletContext().setAttribute("userCount", count);
//        System.out.println("********************会话销毁");
//        Map<String, String> map = (Map<String, String>) event.getSession().getServletContext().getAttribute("sessionMap");
//        String id = event.getSession().getId();
//        Boolean map1 = MapUtilss.findMapKey(map, id);
//        if (map1) {
//            map.remove(id);
//        }
//        event.getSession().getServletContext().setAttribute("sessionMap",map);
//        System.out.println("****************用户："+ map.get(id) +"      从服务器删除");
    }

}
